<template>
  <!-- 登录页面 -->
  <div class="com-page">
    <div class="login-con">
      <!-- 头像开始 -->
      <div class="logo">
        <img src="/static/pic/logo.png" alt="" srcset="" />
      </div>
      <!-- 登录区域 -->
      <div class="form-con">
        <el-form :model="form" ref="loginFormRef" :rules="rules">
          <!-- 用户名 -->
          <el-form-item prop="username">
            <el-input
              v-model="form.username"
              prefix-icon=" iconfont icon-xingmingyonghumingnicheng"
            ></el-input>
          </el-form-item>
          <!-- 密码 -->
          <el-form-item prop="password">
            <el-input
              v-model="form.password"
              prefix-icon="iconfont icon-mima"
              show-password
            ></el-input>
          </el-form-item>
          <el-form-item class="button-con">
            <el-button class="button" type="primary" @click="onSubmit"
              >登录</el-button
            >
            <el-button class="button" @click="resetForm">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script>
import { request } from "@/utils";
export default {
  components: {},
  data() {
    return {
      form: {
        username: "admin",
        password: "123456",
      },
      rules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          { min: 3, max: 5, message: "长度在 3 到 5 个字符", trigger: "blur" },
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          {
            min: 6,
            max: 12,
            message: "长度在 6 到 12 个字符",
            trigger: "blur",
          },
        ],
      },
    };
  },
  watch: {},
  computed: {},
  methods: {
    onSubmit() {
      this.$refs.loginFormRef.validate(async (v) => {
        if (!v) {
          return;
        }
        const { data: ret } = await request.post("login", this.form);
        window.sessionStorage.setItem("token", ret.data.token);
        this.$router.push("/home");
      });
    },
    // 重置按钮功能实现
    resetForm() {
      this.$refs.loginFormRef.resetFields();
    },
  },
  created() {},
  mounted() {},
};
</script>
<style lang="less" scoped>
.com-page {
  height: 100%;
  background-color: #2a3756;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
  .login-con {
    width: 600px;
    height: 400px;
    background-color: #fff;
    // logo布局开始
    .logo {
      width: 160px;
      border-radius: 50%;
      padding: 8px;
      overflow: hidden;
      margin-left: 50%;
      transform: translate(-50%, -30%);
      box-shadow: 0 0 6px #ddd;
      border: 2px solid #eee;
      background-color: #fff;
      img {
        background-color: #eee;
        border-radius: 50%;
      }
    }
    // 表单布局开始
    .form-con {
      padding: 0 30px;
    }
  }
}

// ui组件布局设计
.el-form-item__content {
  margin: 0;
}
.button-con {
  display: flex;
  width: 100%;
  justify-content: flex-end;
  margin-top: 30px;
}
</style>